/**
 * @(#) VacationCodeProxy.as
 *
 * @date		2009. 02. 12.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	휴가/근무 코드 Proxy - Proxy
 * 				휴가/근무와 관련한 코드리스트를 로드
 */
package com.skt.common_hr.work.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.work.WorkFacade;
	import com.skt.common_hr.work.model.vo.VacationCodeVO;
	import com.wcl.dataloader.*;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
		
	public class VacationCodeProxy extends CommonProxy
	{
		public static const NAME:String 				= "VacationCodeProxy";
		public static const LIST_VACATION:String 		= "1";
		public static const LIST_WORKTIME:String		= "2";
		
		public var codeList:ArrayCollection;
		public var vacationList:ArrayCollection;
		public var worktimeList:ArrayCollection;
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function VacationCodeProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load():void
		{
			// 어플리케이션 전 영역에 걸쳐 한번 만 로드한다.
			if(codeList==null) {
				var param:Object = new Object();
				param["I_PERNR"] = Variables.myPernr;						
				loadData("Z_HR_0026", param, result, fault);
			}
			else {
				facade.sendNotification(WorkFacade.LOAD_VACATION_CODE_COMPLETE, this);
			} 
		}
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {  
				codeList = parseCode(event.result.HTAB);
				facade.sendNotification(WorkFacade.LOAD_VACATION_CODE_COMPLETE, this);
			}
			else {
				trace( "failed to load vacation quota: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				facade.sendNotification(WorkFacade.LOAD_VACATION_CODE_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace("Fault message in DateProxy: " + event.fault.faultString);
			facade.sendNotification(WorkFacade.LOAD_VACATION_CODE_FAILED, this);
		}
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 제증명서 종류 Code list
		 */
		private function parseCode(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			vacationList = new ArrayCollection();
			worktimeList = new ArrayCollection();
						
			var vo:VacationCodeVO = new VacationCodeVO;
			vo.code = "";
			vo.text = "";
			vo.oneDay = false;
			retArr.addItem(vo);
			vacationList.addItem(vo);
			worktimeList.addItem(vo);
			for each(var rec:* in srcArr)
			{
				vo = new VacationCodeVO;
				vo.DGBN = rec.DGBN;
				vo.code = rec.AWART;
				vo.text = rec.ATEXT;
				vo.oneDay = (rec.ONEDAY=="X") ? true:false;
				retArr.addItem(vo);
				if(vo.DGBN == "1") {
					vacationList.addItem(vo);
				}
				else {
					worktimeList.addItem(vo);
				}
			}
			return retArr;	
		}
		
	}
}